package com.wbao.dianniu.emojiUtils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;


import com.wbao.dianniu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zejian
 * Time  16/1/6 下午5:26
 * Email shinezejian@163.com
 * Description:表情主界面
 */
public class EmotionMainFragment extends BaseFragment {

    //是否绑定当前Bar的编辑框的flag
    public static final String BIND_TO_EDITTEXT = "bind_to_edittext";
    //是否隐藏bar上的编辑框和发生按钮
    public static final String HIDE_BAR_EDITTEXT_AND_BTN = "hide bar's editText and btn";

    //当前被选中底部tab
    private static final String CURRENT_POSITION_FLAG = "CURRENT_POSITION_FLAG";
    //表情面板
    private EmotionKeyboard mEmotionKeyboard;

    private EditText bar_edit_text;
    private ImageView bar_image_add_btn;
//    private ImageView aFriendIV;
//    private ImageView emojiIV;
//    private ImageView picIV;

    //需要绑定的内容view
    private View contentView;

    //不可横向滚动的ViewPager
    private NoHorizontalScrollerViewPager viewPager;

    //是否绑定当前Bar的编辑框,默认true,即绑定。
    //false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
    private boolean isBindToBarEditText = true;

    //是否隐藏bar上的编辑框和发生按钮,默认不隐藏
    private boolean isHidenBarEditTextAndBtn = false;

    List<Fragment> fragments = new ArrayList<>();


    /**
     * 创建与Fragment对象关联的View视图时调用
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_emotion, container, false);
        isHidenBarEditTextAndBtn = args.getBoolean(EmotionMainFragment.HIDE_BAR_EDITTEXT_AND_BTN);
        //获取判断绑定对象的参数
        isBindToBarEditText = args.getBoolean(EmotionMainFragment.BIND_TO_EDITTEXT);
        initView(rootView);
        mEmotionKeyboard = EmotionKeyboard.with(getActivity())
                .setEmotionView(rootView.findViewById(R.id.ll_emotion_layout))//绑定表情面板
                .bindToContent(contentView)//绑定内容view
                .bindToEditText(bar_edit_text)//判断绑定那种EditView
                .bindToEmotionButton(bar_image_add_btn)//绑定表情按钮
//                .bindToEditText(!isBindToBarEditText ? ((EditText) contentView) : ((EditText) rootView.findViewById(R.id.bar_edit_text)))//判断绑定那种EditView
//                .bindToEmotionButton(rootView.findViewById(R.id.bar_image_add_btn))//绑定表情按钮
                .build();
        replaceFragment();
//        initDatas();
        initListener();
        //创建全局监听
        GlobalOnItemClickManagerUtils globalOnItemClickManager = GlobalOnItemClickManagerUtils.getInstance();
        //绑定当前Bar的编辑框
//        globalOnItemClickManager.attachToEditText(bar_edit_text);

        if (isBindToBarEditText) {
            //绑定当前Bar的编辑框
            globalOnItemClickManager.attachToEditText(bar_edit_text);
        } else {
            // false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
//            globalOnItemClickManager.attachToEditText((EditText) contentView);
//            mEmotionKeyboard.bindToEditText((EditText) contentView);
            //绑定当前Bar的编辑框
            globalOnItemClickManager.attachToEditText(bar_edit_text);
            mEmotionKeyboard.bindToEditText(bar_edit_text);
        }
        return rootView;
    }

    public void removeEditText() {
        GlobalOnItemClickManagerUtils.getInstance().removeEditText(bar_edit_text);
    }

    private void initListener() {

    }

    /**
     * 绑定内容view
     *
     * @param contentView
     * @return
     */
    public void bindToContentView(View contentView, ImageView emojiImage, EditText editText) {
        this.contentView = contentView;
        this.bar_image_add_btn = emojiImage;
        this.bar_edit_text = editText;
    }


    /**
     * 初始化view控件
     */
    protected void initView(View rootView) {
        viewPager = (NoHorizontalScrollerViewPager) rootView.findViewById(R.id.vp_emotionview_layout);
//        emojiIV = (ImageView) rootView.findViewById(R.id.comment_chose_emoji);
//        aFriendIV = (ImageView) rootView.findViewById(R.id.comment_a_friends);
//        picIV = (ImageView) rootView.findViewById(R.id.comment_chose_pic);
//        recyclerview_horizontal = (RecyclerView) rootView.findViewById(R.id.recyclerview_horizontal);
    }

    private void replaceFragment() {
        //创建fragment的工厂类
        FragmentFactory factory = FragmentFactory.getSingleFactoryInstance();
        //创建修改实例
        EmotiomComplateFragment f1 = (EmotiomComplateFragment) factory.getFragment(EmotionUtils.EMOTION_TYPE_PEOPLE);
        f1.bindEditText(bar_edit_text);
//        Bundle bundle = new Bundle();
//        bundle.putInt(FragmentFactory.EMOTION_MAP_TYPE, EmotionUtils.EMOTION_TYPE_PEOPLE);
//        EmotiomComplateFragment f1 = EmotiomComplateFragment.newInstance(EmotiomComplateFragment.class, bundle);
        fragments.add(f1);

        NoHorizontalScrollerVPAdapter adapter = new NoHorizontalScrollerVPAdapter(getActivity().getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
    }


    /**
     * 是否拦截返回键操作，如果此时表情布局未隐藏，先隐藏表情布局
     *
     * @return true则隐藏表情布局，拦截返回键操作
     * false 则不拦截返回键操作
     */
    public boolean isInterceptBackPress() {
        return mEmotionKeyboard.interceptBackPress();
    }

    public boolean onBackPress() {
        return mEmotionKeyboard.onBackPress();
    }

    /**
     * 隐藏表情面板，显示软键盘
     *
     * @return
     */
    public void hideEmotionShowSoftInput() {
        mEmotionKeyboard.hideEmotionShowSoftInput();
    }

}


